﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace HOOMT
{
    class PrimitiveObjectDiagram : ObjectDiagram
    {
        public PrimitiveObjectDiagram(Point point, int width = 140, int height = 90)
            : base(point, width, height)
        {
            name = "PrimitiveObjectDiagram";
        }
        public PrimitiveObjectDiagram(PrimitiveObjectDiagram diagram)
            : base(new Point(), diagram.width, diagram.height)
        {
            this.id = diagram.id;
            this.AssociationEndList = diagram.AssociationEndList;
            this.AssociationStartList = diagram.AssociationStartList;
            this.AttributeList = diagram.AttributeList;
            this.MethodList = diagram.MethodList;
            this.NonFunctionRequirmentList = diagram.NonFunctionRequirmentList;
        }
        //绘图函数
        //************************************************************************************
        /// <summary>
        /// 绘制图形对象本身
        /// 绘制图像对象多包含的文字
        /// 图形对象的大小随着文字量而改变
        /// </summary>
        /// <param name="g"></param>
        /// <param name="m"></param>
        public override void Drawing(Graphics g, OriginalModelForm m)
        {
            Font drawFont = new Font("Arial", 8);
            SolidBrush drawBrush = new SolidBrush(Color.Black);
            Pen pen = new Pen(Color.Brown, 1);
            //Rectangle rect1 = new Rectangle(upLeftpoint.X, upLeftpoint.Y, width, height);
            //g.DrawRectangle(pen, rect);
            //String[] str = MethodList.ToString();
            Point pt = new Point(upLeftpoint.X+10, upLeftpoint.Y+5);//文字显示开始位置距左上角（10，10）单位
            //方法部分
            foreach (Method method in MethodList)
            {
                g.DrawString(getShowString(method.getShowString()), drawFont, drawBrush, pt);
                pt.Y += 10;
            }
            if (pt.Y < upLeftpoint.Y + height / 3)
                pt.Y = upLeftpoint.Y + height / 3;
            pt.Y += 2;//文字显示开始位置距其下面直线2
            g.DrawLine(pen, upLeftpoint.X, pt.Y, upLeftpoint.X + width, pt.Y);
            pt.Y += 5;//文字显示开始位置距其上面直线10
            //属性部分
            foreach (Attribute attr in AttributeList)
            {
                g.DrawString(getShowString(attr.getShowString()), drawFont, drawBrush, pt);
                pt.Y += 10;
            }

            if (pt.Y < upLeftpoint.Y + 2 * height / 3)
                pt.Y = upLeftpoint.Y + 2 * height / 3;
            pt.Y += 2;
            g.DrawLine(pen, upLeftpoint.X, pt.Y, upLeftpoint.X + width, pt.Y);
            pt.Y += 5;
            //非功能需求部分
            foreach (NonFunctionRequirment requirment in NonFunctionRequirmentList)
            {
                g.DrawString(getShowString(requirment.getShowString()), drawFont, drawBrush, pt);
                pt.Y += 10;
            }
            pt.Y += 2;
            if (pt.Y > upLeftpoint.Y + height)
                height = pt.Y - upLeftpoint.Y;
            Rectangle rect = new Rectangle(upLeftpoint.X, upLeftpoint.Y, width, height);
            g.DrawRectangle(pen, rect);
            
            //g.DrawLine(pen, upLeftpoint.X, upLeftpoint.Y + height / 3, upLeftpoint.X + width, upLeftpoint.Y + height / 3);
            //g.DrawLine(pen, upLeftpoint.X, upLeftpoint.Y + 2 * height / 3, upLeftpoint.X + width, upLeftpoint.Y + 2 * height / 3);
            //显示文字
            /*nameBox.Size = new Size(width, 30);
            nameBox.Location = new Point(upLeftpoint.X, upLeftpoint.Y - 15);
            nameBox.Text = name;
            nameBox.BackColor = SystemColors.Control;//设置为与MainForm相同的颜色
            nameBox.BorderStyle = System.Windows.Forms.BorderStyle.None;//取消边框
            m.Controls.Add(nameBox);*/
            base.Drawing(g, m);  
        }
        //************************************************************************************
        //如果字符长度过长，则截取字符的曲面一部分
        //这里，估计每个英文字符长为6个单位，以此获取应该显示的子字符串
        private String getShowString(String str)
        {
            String showString;
            if (str.Length * 6 > width)
                showString = str.ToString().Substring(0, width / 6);
            else
                showString = str.ToString();
            return showString;
        }
        public override int getMinHeight()
        {
            return (MethodList.Count()+AttributeList.Count()+NonFunctionRequirmentList.Count())*9+24;//每天记录占用10个单位的高度，记录与直线的间隔总共21个单位高度
        }
    }
}
